package com.shr.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shr.lease.common.exception.LeaseException;
import com.shr.lease.common.result.ResultCodeEnum;
import com.shr.lease.model.entity.*;
import com.shr.lease.model.enums.ItemType;
import com.shr.lease.web.admin.mapper.*;
import com.shr.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shr.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.shr.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.shr.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.shr.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.shr.lease.web.admin.vo.fee.FeeValueVo;
import com.shr.lease.web.admin.vo.graph.GraphVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private LabelInfoMapper labelInfoMapper;
    @Resource
    private FacilityInfoMapper facilityInfoMapper;
    @Resource
    private FeeValueMapper feeValueMapper;
    @Resource
    private GraphInfoService graphInfoService;
    @Resource
    private ApartmentLabelService apartmentLabelService;
    @Resource
    private ApartmentFacilityService apartmentFacilityService;
    @Resource
    private ApartmentFeeValueService apartmentFeeValueService;
    @Resource
    private RoomInfoService roomInfoService;

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page, queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //公寓基本信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        //图片列表 graphVoList
        List<GraphVo> graphVoList = graphInfoMapper.selectByItemTypeAndItemId(id, ItemType.APARTMENT);
        //标签列表 labelInfoList
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        //配套列表 facilityInfoList
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);
        //杂费列表
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);
        ApartmentDetailVo apartmentDetailVo = ApartmentDetailVo.builder()
                .facilityInfoList(facilityInfoList)
                .graphVoList(graphVoList)
                .labelInfoList(labelInfoList)
                .feeValueVoList(feeValueVoList)
                .build();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        return apartmentDetailVo;
    }

    @Override
    public void saveOrUpdateApartmentVo(ApartmentSubmitVo apartmentSubmitVo) {

        //判断是更新还是保存
        boolean isUpdate = apartmentSubmitVo.getId() != null;

        //先保存或更新公寓基本信息
        this.saveOrUpdate(apartmentSubmitVo);
        Long apartmentId = apartmentSubmitVo.getId();

        if (isUpdate){
            //删除图片信息
            graphInfoMapper.delete(
                    new LambdaQueryWrapper<GraphInfo>()
                            .eq(GraphInfo::getItemId, apartmentId)
                            .eq(GraphInfo::getItemType, ItemType.APARTMENT)
            );
            //删除标签信息
            apartmentLabelService.remove(
                    new LambdaQueryWrapper<ApartmentLabel>()
                            .eq(ApartmentLabel::getApartmentId, apartmentId)
            );
            //删除配套信息
            apartmentFacilityService.remove(
                    new LambdaQueryWrapper<ApartmentFacility>()
                            .eq(ApartmentFacility::getApartmentId, apartmentId)
            );
            //删除费用信息
            apartmentFeeValueService.remove(
                    new LambdaQueryWrapper<ApartmentFeeValue>()
                            .eq(ApartmentFeeValue::getApartmentId, apartmentId)
            );
        }

        //保存图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
//            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
//            for (GraphVo graphVo : graphVoList) {
//                GraphInfo graphInfo = GraphInfo.builder()
//                        .itemId(apartmentId)
//                        .itemType(ItemType.APARTMENT)
//                        .build();
//                BeanUtils.copyProperties(graphVo, graphInfo);
//                graphInfos.add(graphInfo);
//            }
            List<GraphInfo> graphInfos = graphVoList
                    .stream()
                    .map(graphVo -> {
                        GraphInfo graphInfo = GraphInfo.builder()
                                .itemId(apartmentId)
                                .itemType(ItemType.APARTMENT)
                                .build();
                        BeanUtils.copyProperties(graphVo, graphInfo);
                        return graphInfo;
                    })
                    .toList();
                    //.collect(Collectors.toList());
            graphInfoService.saveBatch(graphInfos);
        }

        //保存标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
//            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
//            for (Long labelId : labelIds) {
//                apartmentLabels.add(
//                        ApartmentLabel
//                                .builder()
//                                .apartmentId(apartmentId)
//                                .labelId(labelId)
//                                .build()
//                );
//            }

            //转化为Stream流
            List<ApartmentLabel> apartmentLabels = labelIds.stream()
                    .map(labelId -> ApartmentLabel
                            .builder()
                            .apartmentId(apartmentId)
                            .labelId(labelId)
                            .build())
                    .toList();
                    //.collect(Collectors.toList()); toList()在JDK16以后添加和上述做法相同

            apartmentLabelService.saveBatch(apartmentLabels);
        }

        //保存配套信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
//            List<ApartmentFacility> apartmentFacilities = new ArrayList<>();
//            for (Long facilityInfoId : facilityInfoIds) {
//                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(apartmentId).facilityId(facilityInfoId).build();
//                apartmentFacilities.add(apartmentFacility);
//            }
            //转化为stream流
            List<ApartmentFacility> apartmentFacilities = facilityInfoIds.stream()
                    .map(facilityId -> ApartmentFacility.builder()
                            .facilityId(facilityId)
                            .apartmentId(apartmentId)
                            .build())
                    .toList();
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }

        //保存费用信息

        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
//            ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
//            for (Long feeValueId : feeValueIds) {
//                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue
//                        .builder()
//                        .apartmentId(apartmentId)
//                        .feeValueId(feeValueId)
//                        .build();
//                apartmentFeeValues.add(apartmentFeeValue);
//            }
            //转化为stream流
            List<ApartmentFeeValue> apartmentFeeValues = feeValueIds.stream()
                    .map(feeValueId -> ApartmentFeeValue.builder()
                            .feeValueId(feeValueId)
                            .apartmentId(apartmentId)
                            .build())
                    .toList();
            apartmentFeeValueService.saveBatch(apartmentFeeValues);

        }


    }

    @Override
    public void removeApartmentById(Long apartmentId) {
        //判断公寓中是否还有房间信息，若存在，则无法删除公寓信息
        if (roomInfoService.count(new LambdaQueryWrapper<RoomInfo>()
                .eq(RoomInfo::getApartmentId, apartmentId)) > 0
        ) throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        //删除公寓信息
        this.removeById(apartmentId);
        //删除图片信息
        graphInfoMapper.delete(
                new LambdaQueryWrapper<GraphInfo>()
                        .eq(GraphInfo::getItemId, apartmentId)
                        .eq(GraphInfo::getItemType, ItemType.APARTMENT)
        );
        //删除标签信息
        apartmentLabelService.remove(
                new LambdaQueryWrapper<ApartmentLabel>()
                        .eq(ApartmentLabel::getApartmentId, apartmentId)
        );
        //删除配套信息
        apartmentFacilityService.remove(
                new LambdaQueryWrapper<ApartmentFacility>()
                        .eq(ApartmentFacility::getApartmentId, apartmentId)
        );
        //删除费用信息
        apartmentFeeValueService.remove(
                new LambdaQueryWrapper<ApartmentFeeValue>()
                        .eq(ApartmentFeeValue::getApartmentId, apartmentId)
        );
    }
}




